package com.mtao.impl;

import com.mtao.SysMenuService;
import com.mtao.utils.ThreadLocalUtil;
import com.mtao.mapper.SysMenuMapper;
import com.mtao.mapper.SysRoleMenuMapper;
import com.mtao.entity.SysMenu;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @program: ems-admin-boot
 * @description: this is a class
 * @author: starao
 * @create: 2021-11-27 14:33
 **/
@Service
@RequiredArgsConstructor
public class SysMenuServiceImpl implements SysMenuService {
    @Resource
    private SysRoleMenuMapper roleMenuMapper;
    @Resource
    private SysMenuMapper sysMenuMapper;

    /**
     * 组装树的公共方法
     * @param menuListAll
     * @param id
     * @return
     */
    public List<SysMenu> getObjects(List<SysMenu> menuListAll,Integer id){
        List<SysMenu> sysMenus = new ArrayList<>();
        for (SysMenu child : menuListAll){
            if(child.getParentId().equals(id)){
                List<SysMenu> objects = getObjects(menuListAll,child.getId());
                child.setChildren(objects);
                sysMenus.add(child);
            }
        }
        if(sysMenus.size()>0){
            return sysMenus;
        }else {
            return null;
        }
    }
    /**
     * @Description: 获取菜单列表
     * @Param: [blurry]
     */
    @Override
    public List<SysMenu> getMenuList() {
        List<SysMenu> menuAll = sysMenuMapper.getMenuAll();
        List<SysMenu> sysMenus = getObjects(menuAll,0);
        return sysMenus;
    }

    /**
     * @Description: 添加菜单
     * @Param: [sysMenu]
     * @return: void
     * @Author: starao
     * @Date: 2022/10/6
     */
    @Override
    public void addMenu(SysMenu sysMenu) {
        if(sysMenu.getParentId()==null){
            sysMenu.setParentId(0);
        }
        sysMenuMapper.addMenu(sysMenu);
    }

    /**
     * @Description: 获取权限列表
     * @Param: []
     */
    @Override
    public List<String> getPermission(Integer id) {
        System.out.println(id);
        Map<String,Object> map= (Map<String,Object>) ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("userId");
        return null;
    }
    /**
     * @Description: 获取角色菜单
     * @Param: []
     * @return: java.util.List<com.mtao.pojo.SysMenu>
     */
    @Override
    public List<SysMenu> getRoleIdMenu(List<Integer> rolesIds) {
        List<SysMenu> sysMenusAll =sysMenuMapper.queryRoleMenu(rolesIds);
        List<SysMenu> sysMenus = getObjects(sysMenusAll,0);
        return sysMenus;
    }
    /**
     * @Description: 删除菜单
     * @Param: [id]
     * @return: void
     */
    @Override
    public void delMenu(Integer id) {
        sysMenuMapper.delMenu(id);
    }
    /**
     * @Description: 编辑菜单
     * @Param: [sysMenu]
     * @return: void
     */
    @Override
    public void editMenu(SysMenu sysMenu) {
        sysMenuMapper.editMenu(sysMenu);
    }

}
